package com.bw.inspection.page

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bw.a_zg3_zxm01.park.xjgl.xjXq.BzAdapter
import com.bw.a_zg3_zxm01.park.xjgl.xjXq.GdAdapter
import com.bw.base.path.RouterPath
import com.bw.common.view.BaseMvvmActivity
import com.bw.inspection.R
import com.bw.inspection.databinding.ActivityInspectionBinding
import com.bw.inspection.databinding.ActivityInspectionDetailBinding
import com.bw.inspection.vm.InspectionViewModel
import com.bw.server.adapter.DeviceAdapter
import com.bw.server.vm.ServerViewModel
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayout.OnTabSelectedListener
import dagger.hilt.android.AndroidEntryPoint

/**
 * 点位详情
 */
@Route(path = RouterPath.PATH_INSPECTION_DETAIL_PATH)
@AndroidEntryPoint
class InspectionDetailActivity : BaseMvvmActivity<ActivityInspectionDetailBinding>() {
    @JvmField
    @Autowired
    var xid = 0
    var bzAdapter = BzAdapter()
    var gdAdapter = GdAdapter()
    var xjjlAdapter = DeviceAdapter()
    override fun getLayoutId(): Int = R.layout.activity_inspection_detail
    private val mViewModel :InspectionViewModel by viewModels()
    private val mmViewModel:ServerViewModel by viewModels()

    override fun initViews() {
        super.initViews()
        ARouter.getInstance().inject(this)
        mDataBinding.back.setOnClickListener { finish() }
        mViewModel.getInspectionDetailData(xid)
        val list = listOf<String>(
            "1      绿地是否需要修剪",
            "2      现场是否有垃圾",
            "3      是否有安全隐患",
            "4      设施是否有损坏"
        )
        mDataBinding.rv.adapter = bzAdapter
        bzAdapter.addData(list)
        mmViewModel.getDeviceData()



        mDataBinding.tab.addTab(mDataBinding.tab.newTab().setText("巡检标准"))
        mDataBinding.tab.addTab(mDataBinding.tab.newTab().setText("巡检记录"))
        mDataBinding.tab.addTab(mDataBinding.tab.newTab().setText("工单记录"))
        mDataBinding.tab.addOnTabSelectedListener(object: OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab?) {
                when(tab?.position){
                    0->{
                        mDataBinding.rv.adapter = bzAdapter
                    }
                    1->{
                        mDataBinding.rv.adapter = xjjlAdapter
                        xjjlAdapter.setOnItemClickListener { adapter, view, position ->
                            ARouter.getInstance().build(RouterPath.PATH_DEVICE_DETAIL_NORMAL)
                                .withInt("did",xjjlAdapter.data[position].id)
                                .navigation()
                        }
                    }
                    2->{
                        val list1 = listOf<String>(
                            "暂无数据",
                        )
                        mDataBinding.rv.adapter = gdAdapter
                        gdAdapter.data.clear()
                        gdAdapter.addData(list1)
                        gdAdapter.notifyDataSetChanged()

                    }
                }
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {
            }

            override fun onTabReselected(tab: TabLayout.Tab?) {
            }

        })


        //跳转到巡检代办
        mDataBinding.addDevice.setOnClickListener {
            ARouter.getInstance().build(RouterPath.PATH_INSPECTION_NO_PATH).navigation()
        }
    }

    override fun loadData() {
        super.loadData()
        mViewModel.mInspectionDetailSuccess.observe(this){
            mDataBinding.xmName.setText("项目名称：${it.projectId}")
            mDataBinding.dwName.setText("点位名称：${it.pointName}")
            mDataBinding.dwwz.setText("点位位置：${it.pointLocation}")
            mDataBinding.type.text = it.patrolType//状态
        }
        mmViewModel.mSuccess.observe( this){
            xjjlAdapter.data.clear()
            xjjlAdapter.addData(it.reversed())
            xjjlAdapter.notifyDataSetChanged()
        }



    }

}